package com.eascs.paymentdetail.controller;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.Format;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.eascs.admin.o2o.internal.entity.LoanapplyExpand;
import com.eascs.admin.o2o.internal.service.LoanapplyExpandService;
import com.eascs.admin.o2o.internal.util.StatusContexts;
import com.eascs.admin.o2o.loan.entity.O2oLoanapplyEntity;
import com.eascs.admin.o2o.loan.service.O2oLoanapplyService;
import com.eascs.app.dao.PageResult;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.util.StringUtil;
import com.eascs.common.util.UrlUtil;
import com.eascs.paymentdetail.entity.O2oPaymentDetailEntity;
import com.eascs.paymentdetail.entity.OaPaymentLoanExpand;
import com.eascs.paymentdetail.service.O2oPaymentDetailService;
import com.eascs.paymentdetail.vo.O2oPaymentDetailPageVo;
import com.eascs.paymentdetail.vo.O2oPaymentDetailVo;
import com.eascs.paymentdetail.vo.O2oPaymentForm;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.page.SelectDataOption;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.HtmlRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.web.sso.User;
import com.eascs.web.sso.UserHolder;

/**
 * @ClassName: O2oPaymentDetailController.java
 * @Description:
 * 				<p>
 *               财务收付详细信息 Controller
 *               </p>
 * @author CXW
 * @date 2016年9月9日 下午3:05:03
 * @version V1.0
 */
@RestController
@RequestMapping("/rpaymentd")
public class O2oPaymentDetailRestController extends BaseRestController {
	private static Logger log = LoggerFactory.getLogger(O2oPaymentDetailRestController.class);

	@Autowired
	private O2oLoanapplyService o2oLoanapplyService;

	@Autowired
	private LoanapplyExpandService loanapplyExpandService;

	@Autowired
	O2oPaymentDetailService o2oPaymentDetailService;

	/**
	 * @Description:
	 * 				<p>
	 *               显示财务收付详细信息
	 *               </p>
	 * @author CXW
	 * @date 2016年9月9日 下午4:17:14
	 * @version V1.0
	 * @param billCode,loanapplyNo
	 * @return BaseRestResult<String>
	 */
	@RequestMapping("/detail")
	public BaseRestResult<String> paymentDetailView(String loanapplyNo) {
		O2oLoanapplyEntity loan = o2oLoanapplyService.findById(loanapplyNo);
		O2oPaymentDetailVo o2oPaymentDetailVo = o2oPaymentDetailService.findByBillCodeOrLoanapplyNo(loanapplyNo);
		// 查询是否已交保证金、管理费、风险保证金
		ViewContext.put("loanBondStatus", loanapplyExpandService.queryLoanapplyNo(loanapplyNo));
		ViewContext.put("loan", loan);
		ViewContext.put("vo", o2oPaymentDetailVo);
		return HtmlRestResult.html("paymentdetail/paymentdetailView");
	}

	/**
	 * 根据贷款单号查询收支明细
	 * 
	 * @param vo
	 * @return
	 */
	@RequestMapping("/index")
	public BaseRestResult<String> index(O2oPaymentDetailPageVo vo) {
		vo.setPageSize(Integer.MAX_VALUE);
		PageResult<Object[]> pageResult = o2oPaymentDetailService.findByPage(vo);
		log.info("result info data:{}", pageResult.getData());
		String titles[] = { "单号", "收付类型", "科目", "金额", "发生时间", "记录时间", "操作人", "发生来源", "备注" };
		PaginationInfo paginationInfo = new PaginationInfo(pageResult, titles);
		paginationInfo.setBuildPagingNavigation(false);
		paginationInfo.setFirstRenderColumnIndex(0);
		SelectDataOption[] selectDataOptions = { null, new SelectDataOption("PAYMENT"), new SelectDataOption("SUBJECT"), null, null, null, null, null };
		paginationInfo.setSelectDataOptions(selectDataOptions);
		paginationInfo.setFormats(new Format[] { null, null, null, new DecimalFormat(",##0.00"), null, null, null, null });
		ViewContext.put("paginationInfo", paginationInfo);
		log.info("result info {}", paginationInfo);
		return HtmlRestResult.popHtmlFragment("paymentdetail/index");
	}

	/**
	 * 红冲页面
	 * 
	 * @param vo
	 * @return
	 */
	@RequestMapping("/redView")
	public BaseRestResult<String> redView() {
		log.info("redView info 红冲弹出界面");
		return HtmlRestResult.popHtmlFragment("paymentdetail/red");
	}

	/**
	 * 蓝补页面
	 * 
	 * @param vo
	 * @return
	 */
	@RequestMapping("/blueView")
	public BaseRestResult<String> blueView() {
		log.info("redView info 蓝补弹出界面");
		return HtmlRestResult.popHtmlFragment("paymentdetail/blue");
	}

	/**
	 * 红冲
	 * 
	 * @param vo
	 * @return
	 */
	@RequestMapping("/red")
	public BaseRestResult<String> red(O2oPaymentForm o2oPaymentForm) {
		log.info("redView info 红冲开始");
		String url = "/rpaymentd/blueRed";
		String billCode = o2oPaymentForm.getBillCode() == null ? "" : o2oPaymentForm.getBillCode();
		String subject = o2oPaymentForm.getSubject() == null ? "" : o2oPaymentForm.getSubject();
		BigDecimal account = o2oPaymentForm.getAccount() == null ? new BigDecimal(0) : o2oPaymentForm.getAccount();
		String source = o2oPaymentForm.getSource() == null ? "" : o2oPaymentForm.getSource();
		String remark = o2oPaymentForm.getRemark() == null ? "" : o2oPaymentForm.getRemark();
		String paymentType = o2oPaymentForm.getPaymentType() == null ? "" : o2oPaymentForm.getPaymentType();
		url = UrlUtil.appendUrl(url, "billCode", billCode);
		url = UrlUtil.appendUrl(url, "subject", subject);
		url = UrlUtil.appendUrl(url, "account", account.toString());
		url = UrlUtil.appendUrl(url, "source", source);
		url = UrlUtil.appendUrl(url, "remark", remark);
		url = UrlUtil.appendUrl(url, "paymentType", paymentType);
		return BaseRestResult.confirm("确认红冲吗？确认提交后数据不能修改。").andActionAjaxLoad(url);
	}

	/**
	 * 蓝补
	 * 
	 * @param vo
	 * @return
	 */
	@RequestMapping("/blue")
	public BaseRestResult<String> blue(O2oPaymentForm o2oPaymentForm) {
		log.info("redView info 蓝补开始");
		String url = "/rpaymentd/blueRed";
		String billCode = o2oPaymentForm.getBillCode() == null ? "" : o2oPaymentForm.getBillCode();
		String subject = o2oPaymentForm.getSubject() == null ? "" : o2oPaymentForm.getSubject();
		BigDecimal account = o2oPaymentForm.getAccount() == null ? new BigDecimal(0) : o2oPaymentForm.getAccount();
		String source = o2oPaymentForm.getSource() == null ? "" : o2oPaymentForm.getSource();
		String remark = o2oPaymentForm.getRemark() == null ? "" : o2oPaymentForm.getRemark();
		String paymentType = o2oPaymentForm.getPaymentType() == null ? "" : o2oPaymentForm.getPaymentType();
		url = UrlUtil.appendUrl(url, "billCode", billCode);
		url = UrlUtil.appendUrl(url, "subject", subject);
		url = UrlUtil.appendUrl(url, "account", account.toString());
		url = UrlUtil.appendUrl(url, "source", source);
		url = UrlUtil.appendUrl(url, "remark", remark);
		url = UrlUtil.appendUrl(url, "paymentType", paymentType);
		return BaseRestResult.confirm("确认蓝补吗？确认提交后数据不能修改。").andActionAjaxLoad(url);
	}

	/**
	 * @Description:
	 * 				<p>
	 *               红冲蓝补校验方法
	 *               </p>
	 * @author CXW
	 * @date 2016年10月13日 上午11:18:17
	 * @version V1.0
	 */
	@RequestMapping("/blueRed")
	public BaseRestResult<String> blueRed(O2oPaymentForm o2oPaymentForm) {
		LoanapplyExpand loanapplyExpand = getLoanapplyExpan(o2oPaymentForm.getBillCode());
		if (null == loanapplyExpand) {
			return BaseRestResult.warn("保存失败，单号数据异常，请联系运维人员");
		}
		if (o2oPaymentForm.getPaymentType().equals(StatusContexts.PAYMENT_TYPE_XS)) {
			return update(o2oPaymentForm, loanapplyExpand);
		} else {
			if (StringUtil.isNotBlank(loanapplyExpand.getBillStatus())) {
				if (loanapplyExpand.getBillStatus().equals(StatusContexts.O2O_BANK_RUN)) {
					return BaseRestResult.warn("该单号在缴纳保证金流程中，不能红冲");
				}
				if (loanapplyExpand.getBillStatus().equals(StatusContexts.O2O_BANK_NEW)) {
					return BaseRestResult.warn("该单号未缴纳保证金，不能红冲");
				} else {
					return update(o2oPaymentForm, loanapplyExpand);
				}
			} else {
				return BaseRestResult.warn("保存失败，单号数据异常（没有费用状态），请联系运维人员");
			}
		}
	}

	/**
	 * @Description:
	 * 				<p>
	 *               查询保证金管理费等信息
	 *               </p>
	 * @author CXW
	 * @date 2016年10月10日 下午4:23:45
	 * @version V1.0
	 */
	public LoanapplyExpand getLoanapplyExpan(String billCode) {
		O2oLoanapplyEntity loanapply = o2oLoanapplyService.fingByBillCode(billCode);
		if (StringUtil.isNull(loanapply.getLoanapplyNo())) {
			return null;
		}
		LoanapplyExpand loanapplyExpand = loanapplyExpandService.queryLoanapplyNo(loanapply.getLoanapplyNo());
		return loanapplyExpand;
	}

	/**
	 * 联想贷款编号和用户名字
	 * 
	 * @param o2oSearchcustomerName
	 * @return
	 */
	@RequestMapping(value = "/billcode")
	public BaseRestResult<List<Object[]>> getBillde(String billCodeName) {
		List<Object[]> data = new ArrayList<>();
		if (StringUtil.hasText(billCodeName)) {
			data = o2oPaymentDetailService.getBilcodeList(billCodeName);
		}
		return BaseRestResult.buildRestResult(data);
	}

	/**
	 * @Description:
	 * 				<p>
	 *               红冲蓝补新系统更新方法
	 *               </p>
	 * @author CXW
	 * @date 2016年10月12日 下午4:06:01
	 * @version V1.0
	 */
	public BaseRestResult<String> update(O2oPaymentForm o2oPaymentForm, LoanapplyExpand loanapplyExpand) {
		O2oPaymentDetailEntity o2oPaymentDetailEntity = getPaymentEntity(o2oPaymentForm, loanapplyExpand.getLoanapply_no());
		if (o2oPaymentForm.getSubject().equals(StatusContexts.SUBJECT_TYPE_BZJ)) {// 保证金
			if (o2oPaymentForm.getPaymentType().equals(StatusContexts.PAYMENT_TYPE_XF)) {
				o2oPaymentDetailEntity.setRemark("系统单号:" + o2oPaymentForm.getBillCode() + ",红冲保证金：" + o2oPaymentForm.getAccount() + "  ；" + o2oPaymentForm.getRemark());
				BigDecimal yiJiao = loanapplyExpand.getRedDeposite() == null ? new BigDecimal(0) : loanapplyExpand.getRedDeposite();// 已交保证金
				BigDecimal yiTui = loanapplyExpand.getRetaDeposite() == null ? new BigDecimal(0) : loanapplyExpand.getRetaDeposite();// 已退保证金
				BigDecimal xianTui = o2oPaymentForm.getAccount();// 现退保证金金额
				BigDecimal yiXTui = yiTui.add(xianTui);// 现退保证金金额加上已退保证金等于一共退的保证金金额
				int bond = yiXTui.compareTo(yiJiao);// 共退保证金金额是否大于已交保证金
				if (bond == 1) {// 如果大于，则返回
					return BaseRestResult.warn("红冲金额加上已退金额不能大于已交保证金额度");
				} else {// 不大于更新数据库并向明细表中添加数据
					loanapplyExpand.setRetaDeposite(yiXTui);
					o2oPaymentDetailEntity.setRedBlueType(StatusContexts.PAYMENT_TYPE_HC);
					int i = o2oPaymentDetailService.updateNewSysEntityById(o2oPaymentDetailEntity, loanapplyExpand);
					if (i > 0) {
						return BaseRestResult.info("红冲成功").andRefresh();
					} else {
						return BaseRestResult.warn("红冲失败");
					}
				}
			}
			if (o2oPaymentForm.getPaymentType().equals(StatusContexts.PAYMENT_TYPE_XS)) {
				o2oPaymentDetailEntity.setRemark("系统单号:" + o2oPaymentForm.getBillCode() + ",蓝补保证金：" + o2oPaymentForm.getAccount() + "  ；" + o2oPaymentForm.getRemark());
				BigDecimal yiJiao = loanapplyExpand.getRedDeposite() == null ? new BigDecimal(0) : loanapplyExpand.getRedDeposite();// 已交保证金
				BigDecimal xianTui = o2oPaymentForm.getAccount();// 现交保证金金额
				BigDecimal yiXTui = yiJiao.add(xianTui);// 共交保证金
				loanapplyExpand.setRedDeposite(yiXTui);
				o2oPaymentDetailEntity.setRedBlueType(StatusContexts.PAYMENT_TYPE_LB);
				int i = o2oPaymentDetailService.updateNewSysEntityById(o2oPaymentDetailEntity, loanapplyExpand);
				if (i > 0) {
					return BaseRestResult.info("蓝补成功").andRefresh();
				} else {
					return BaseRestResult.warn("蓝补失败");
				}
			}
		} else if (o2oPaymentForm.getSubject().equals(StatusContexts.SUBJECT_TYPE_GLF)) {// 管理费
			if (o2oPaymentForm.getPaymentType().equals(StatusContexts.PAYMENT_TYPE_XF)) {
				o2oPaymentDetailEntity.setRemark("系统单号:" + o2oPaymentForm.getBillCode() + ",红冲管理费：" + o2oPaymentForm.getAccount() + "  ；" + o2oPaymentForm.getRemark());
				BigDecimal yiJiao = loanapplyExpand.getRedManger() == null ? new BigDecimal(0) : loanapplyExpand.getRedManger();// 已交管理费
				BigDecimal yiTui = loanapplyExpand.getRetaManage() == null ? new BigDecimal(0) : loanapplyExpand.getRetaManage();// 已退管理费
				BigDecimal xianTui = o2oPaymentForm.getAccount();// 现退管理费金额
				BigDecimal yiXTui = yiTui.add(xianTui);// 现退管理费金额加上已退管理费等于一共退的管理费金额
				int bond = yiXTui.compareTo(yiJiao);// 共退管理费金额是否大于已交管理费
				if (bond == 1) {// 如果大于，则返回
					return BaseRestResult.warn("红冲金额加上已退金额不能大于已交管理费额度");
				} else {// 不大于更新数据库并向明细表中添加数据
					loanapplyExpand.setRetaManage(yiXTui);
					o2oPaymentDetailEntity.setRedBlueType(StatusContexts.PAYMENT_TYPE_HC);
					int i = o2oPaymentDetailService.updateNewSysEntityById(o2oPaymentDetailEntity, loanapplyExpand);
					if (i > 0) {
						return BaseRestResult.info("红冲成功").andRefresh();
					} else {
						return BaseRestResult.warn("红冲失败");
					}
				}
			}
			if (o2oPaymentForm.getPaymentType().equals(StatusContexts.PAYMENT_TYPE_XS)) {
				o2oPaymentDetailEntity.setRemark("系统单号:" + o2oPaymentForm.getBillCode() + ",蓝补管理费：" + o2oPaymentForm.getAccount() + "  ；" + o2oPaymentForm.getRemark());
				BigDecimal yiJiao = loanapplyExpand.getRedManger() == null ? new BigDecimal(0) : loanapplyExpand.getRedManger();// 已交管理费
				BigDecimal xianTui = o2oPaymentForm.getAccount();// 现交管理费金额
				BigDecimal yiXTui = yiJiao.add(xianTui);// 共交管理费
				loanapplyExpand.setRedManger(yiXTui);
				o2oPaymentDetailEntity.setRedBlueType(StatusContexts.PAYMENT_TYPE_LB);
				int i = o2oPaymentDetailService.updateNewSysEntityById(o2oPaymentDetailEntity, loanapplyExpand);
				if (i > 0) {
					return BaseRestResult.info("蓝补成功").andRefresh();
				} else {
					return BaseRestResult.warn("蓝补失败");
				}
			}
		} else if (o2oPaymentForm.getSubject().equals(StatusContexts.SUBJECT_TYPE_FXBCJ)) {// 风险补偿金
			if (o2oPaymentForm.getPaymentType().equals(StatusContexts.PAYMENT_TYPE_XF)) {
				o2oPaymentDetailEntity.setRemark("系统单号:" + o2oPaymentForm.getBillCode() + ",红冲风险补偿金：" + o2oPaymentForm.getAccount() + "  ；" + o2oPaymentForm.getRemark());
				BigDecimal yiJiao = loanapplyExpand.getRedRisk() == null ? new BigDecimal(0) : loanapplyExpand.getRedRisk();// 已交风险补偿金
				BigDecimal yiTui = loanapplyExpand.getRetaRisk() == null ? new BigDecimal(0) : loanapplyExpand.getRetaRisk();// 已退风险补偿金
				BigDecimal xianTui = o2oPaymentForm.getAccount();// 现退风险补偿金金额
				BigDecimal yiXTui = yiTui.add(xianTui);// 现退风险补偿金金额加上已退风险补偿金等于一共退的风险补偿金金额
				int bond = yiXTui.compareTo(yiJiao);// 共退风险补偿金金额是否大于已交风险补偿金
				if (bond == 1) {// 如果大于，则返回
					return BaseRestResult.warn("红冲金额加上已退金额不能大于已交风险补偿金额度");
				} else {// 不大于更新数据库并向明细表中添加数据
					loanapplyExpand.setRetaRisk(yiXTui);
					o2oPaymentDetailEntity.setRedBlueType(StatusContexts.PAYMENT_TYPE_HC);
					int i = o2oPaymentDetailService.updateNewSysEntityById(o2oPaymentDetailEntity, loanapplyExpand);
					if (i > 0) {
						return BaseRestResult.info("红冲成功").andRefresh();
					} else {
						return BaseRestResult.warn("红冲失败");
					}
				}
			}
			if (o2oPaymentForm.getPaymentType().equals(StatusContexts.PAYMENT_TYPE_XS)) {
				o2oPaymentDetailEntity.setRemark("系统单号:" + o2oPaymentForm.getBillCode() + ",蓝补风险补偿金：" + o2oPaymentForm.getAccount() + "  ；" + o2oPaymentForm.getRemark());
				BigDecimal yiJiao = loanapplyExpand.getRedRisk() == null ? new BigDecimal(0) : loanapplyExpand.getRedRisk();// 已交风险补偿金
				BigDecimal xianTui = o2oPaymentForm.getAccount();// 现交风险补偿金
				BigDecimal yiXTui = yiJiao.add(xianTui);// 共交风险补偿金
				loanapplyExpand.setRedRisk(yiXTui);
				o2oPaymentDetailEntity.setRedBlueType(StatusContexts.PAYMENT_TYPE_LB);
				int i = o2oPaymentDetailService.updateNewSysEntityById(o2oPaymentDetailEntity, loanapplyExpand);
				if (i > 0) {
					return BaseRestResult.info("蓝补成功").andRefresh();
				} else {
					return BaseRestResult.warn("蓝补失败");
				}
			}
		}
		return BaseRestResult.warn("红冲失败");
	}

	/**
	 * @Description:
	 * 				<p>
	 *               红冲蓝补老系统更新方法
	 *               </p>
	 * @author CXW
	 * @date 2016年10月12日 下午4:06:01
	 * @version V1.0
	 */
	public BaseRestResult<String> updateOa(O2oPaymentForm o2oPaymentForm, OaPaymentLoanExpand oaPaymentLoanExpand) {
		O2oPaymentDetailEntity o2oPaymentDetailEntity = getPaymentEntity(o2oPaymentForm, oaPaymentLoanExpand.getLoanapplyNo());
		if (o2oPaymentForm.getSubject().equals(StatusContexts.SUBJECT_TYPE_BZJ)) {// 保证金
			if (o2oPaymentForm.getPaymentType().equals(StatusContexts.PAYMENT_TYPE_XF)) {
				o2oPaymentDetailEntity.setRemark("系统单号:" + o2oPaymentForm.getBillCode() + ",红冲保证金：" + o2oPaymentForm.getAccount() + "  ；" + o2oPaymentForm.getRemark());
				BigDecimal yiJiao = oaPaymentLoanExpand.getRedDeposite() == null ? new BigDecimal(0) : oaPaymentLoanExpand.getRedDeposite();// 已交保证金
				BigDecimal yiTui = oaPaymentLoanExpand.getRetaDeposite() == null ? new BigDecimal(0) : oaPaymentLoanExpand.getRetaDeposite();// 已退保证金
				BigDecimal xianTui = o2oPaymentForm.getAccount();// 现退保证金金额
				BigDecimal yiXTui = yiTui.add(xianTui);// 现退保证金金额加上已退保证金等于一共退的保证金金额
				int bond = yiXTui.compareTo(yiJiao);// 共退保证金金额是否大于已交保证金
				if (bond == 1) {// 如果大于，则返回
					return BaseRestResult.warn("红冲金额加上已退金额不能大于已交保证金额度");
				} else {// 不大于更新数据库并向明细表中添加数据
					oaPaymentLoanExpand.setRetaDeposite(yiXTui);
					o2oPaymentDetailEntity.setRedBlueType(StatusContexts.PAYMENT_TYPE_HC);
					int i = o2oPaymentDetailService.updateOaSysEntityById(o2oPaymentDetailEntity, oaPaymentLoanExpand);
					if (i > 0) {
						return BaseRestResult.info("红冲成功").andRefresh();
					} else {
						return BaseRestResult.warn("红冲失败");
					}
				}
			}
			if (o2oPaymentForm.getPaymentType().equals(StatusContexts.PAYMENT_TYPE_XS)) {
				o2oPaymentDetailEntity.setRemark("系统单号:" + o2oPaymentForm.getBillCode() + ",蓝补保证金：" + o2oPaymentForm.getAccount() + "  ；" + o2oPaymentForm.getRemark());
				BigDecimal yiJiao = oaPaymentLoanExpand.getRedDeposite() == null ? new BigDecimal(0) : oaPaymentLoanExpand.getRedDeposite();// 已交保证金
				BigDecimal xianTui = o2oPaymentForm.getAccount();// 现交保证金金额
				BigDecimal yiXTui = yiJiao.add(xianTui);// 共交保证金
				oaPaymentLoanExpand.setRedDeposite(yiXTui);
				o2oPaymentDetailEntity.setRedBlueType(StatusContexts.PAYMENT_TYPE_LB);
				int i = o2oPaymentDetailService.updateOaSysEntityById(o2oPaymentDetailEntity, oaPaymentLoanExpand);
				if (i > 0) {
					return BaseRestResult.info("蓝补成功").andRefresh();
				} else {
					return BaseRestResult.warn("蓝补失败");
				}
			}
		} else if (o2oPaymentForm.getSubject().equals(StatusContexts.SUBJECT_TYPE_GLF)) {// 管理费
			if (o2oPaymentForm.getPaymentType().equals(StatusContexts.PAYMENT_TYPE_XF)) {
				o2oPaymentDetailEntity.setRemark("系统单号:" + o2oPaymentForm.getBillCode() + ",红冲管理费：" + o2oPaymentForm.getAccount() + "  ；" + o2oPaymentForm.getRemark());
				BigDecimal yiJiao = oaPaymentLoanExpand.getRedManger() == null ? new BigDecimal(0) : oaPaymentLoanExpand.getRedManger();// 已交管理费
				BigDecimal yiTui = oaPaymentLoanExpand.getRetaManage() == null ? new BigDecimal(0) : oaPaymentLoanExpand.getRetaManage();// 已退管理费
				BigDecimal xianTui = o2oPaymentForm.getAccount();// 现退管理费金额
				BigDecimal yiXTui = yiTui.add(xianTui);// 现退管理费金额加上已退管理费等于一共退的管理费金额
				int bond = yiXTui.compareTo(yiJiao);// 共退管理费金额是否大于已交管理费
				if (bond == 1) {// 如果大于，则返回
					return BaseRestResult.warn("红冲金额加上已退金额不能大于已交管理费额度");
				} else {// 不大于更新数据库并向明细表中添加数据
					oaPaymentLoanExpand.setRetaManage(yiXTui);
					o2oPaymentDetailEntity.setRedBlueType(StatusContexts.PAYMENT_TYPE_HC);
					int i = o2oPaymentDetailService.updateOaSysEntityById(o2oPaymentDetailEntity, oaPaymentLoanExpand);
					if (i > 0) {
						return BaseRestResult.info("红冲成功").andRefresh();
					} else {
						return BaseRestResult.warn("红冲失败");
					}
				}
			}
			if (o2oPaymentForm.getPaymentType().equals(StatusContexts.PAYMENT_TYPE_XS)) {
				o2oPaymentDetailEntity.setRemark("系统单号:" + o2oPaymentForm.getBillCode() + ",蓝补管理费：" + o2oPaymentForm.getAccount() + "  ；" + o2oPaymentForm.getRemark());
				BigDecimal yiJiao = oaPaymentLoanExpand.getRedManger() == null ? new BigDecimal(0) : oaPaymentLoanExpand.getRedManger();// 已交管理费
				BigDecimal xianTui = o2oPaymentForm.getAccount();// 现交管理费金额
				BigDecimal yiXTui = yiJiao.add(xianTui);// 共交管理费
				oaPaymentLoanExpand.setRedManger(yiXTui);
				o2oPaymentDetailEntity.setRedBlueType(StatusContexts.PAYMENT_TYPE_LB);
				int i = o2oPaymentDetailService.updateOaSysEntityById(o2oPaymentDetailEntity, oaPaymentLoanExpand);
				if (i > 0) {
					return BaseRestResult.info("蓝补成功").andRefresh();
				} else {
					return BaseRestResult.warn("蓝补失败");
				}
			}
		} else if (o2oPaymentForm.getSubject().equals(StatusContexts.SUBJECT_TYPE_FXBCJ)) {// 风险补偿金
			if (o2oPaymentForm.getPaymentType().equals(StatusContexts.PAYMENT_TYPE_XF)) {
				o2oPaymentDetailEntity.setRemark("系统单号:" + o2oPaymentForm.getBillCode() + ",红冲风险补偿金：" + o2oPaymentForm.getAccount() + "  ；" + o2oPaymentForm.getRemark());
				BigDecimal yiJiao = oaPaymentLoanExpand.getRedRisk() == null ? new BigDecimal(0) : oaPaymentLoanExpand.getRedRisk();// 已交风险补偿金
				BigDecimal yiTui = oaPaymentLoanExpand.getRetaRisk() == null ? new BigDecimal(0) : oaPaymentLoanExpand.getRetaRisk();// 已退风险补偿金
				BigDecimal xianTui = o2oPaymentForm.getAccount();// 现退风险补偿金金额
				BigDecimal yiXTui = yiTui.add(xianTui);// 现退风险补偿金金额加上已退风险补偿金等于一共退的风险补偿金金额
				int bond = yiXTui.compareTo(yiJiao);// 共退风险补偿金金额是否大于已交风险补偿金
				if (bond == 1) {// 如果大于，则返回
					return BaseRestResult.warn("红冲金额加上已退金额不能大于已交风险补偿金额度");
				} else {// 不大于更新数据库并向明细表中添加数据
					oaPaymentLoanExpand.setRetaRisk(yiXTui);
					o2oPaymentDetailEntity.setRedBlueType(StatusContexts.PAYMENT_TYPE_HC);
					int i = o2oPaymentDetailService.updateOaSysEntityById(o2oPaymentDetailEntity, oaPaymentLoanExpand);
					if (i > 0) {
						return BaseRestResult.info("红冲成功").andRefresh();
					} else {
						return BaseRestResult.warn("红冲失败");
					}
				}
			}
			if (o2oPaymentForm.getPaymentType().equals(StatusContexts.PAYMENT_TYPE_XS)) {
				o2oPaymentDetailEntity.setRemark("系统单号:" + o2oPaymentForm.getBillCode() + ",蓝补风险补偿金：" + o2oPaymentForm.getAccount() + "  ；" + o2oPaymentForm.getRemark());
				BigDecimal yiJiao = oaPaymentLoanExpand.getRedRisk() == null ? new BigDecimal(0) : oaPaymentLoanExpand.getRedRisk();// 已交风险补偿金
				BigDecimal xianTui = o2oPaymentForm.getAccount();// 现交风险补偿金金额
				BigDecimal yiXTui = yiJiao.add(xianTui);// 共交风险补偿金
				oaPaymentLoanExpand.setRedRisk(yiXTui);
				o2oPaymentDetailEntity.setRedBlueType(StatusContexts.PAYMENT_TYPE_LB);
				int i = o2oPaymentDetailService.updateOaSysEntityById(o2oPaymentDetailEntity, oaPaymentLoanExpand);
				if (i > 0) {
					return BaseRestResult.info("蓝补成功").andRefresh();
				} else {
					return BaseRestResult.warn("蓝补失败");
				}
			}
		}
		return BaseRestResult.warn("红冲失败");
	}

	/**
	 * @Description:
	 * 				<p>
	 *               封装明细实体类，获取操作人信息
	 *               </p>
	 * @author CXW
	 * @date 2016年10月13日 上午10:46:57
	 * @version V1.0
	 */
	public O2oPaymentDetailEntity getPaymentEntity(O2oPaymentForm o2oPaymentForm, String loanapplyNo) {
		User user = UserHolder.getUser();
		Date date = new Date();
		O2oPaymentDetailEntity o2oPaymentDetailEntity = new O2oPaymentDetailEntity();
		o2oPaymentDetailEntity.setAmountMoney(o2oPaymentForm.getAccount());
		o2oPaymentDetailEntity.setBillCode(o2oPaymentForm.getBillCode());
		o2oPaymentDetailEntity.setHappenSource(o2oPaymentForm.getSource());
		o2oPaymentDetailEntity.setLoanapplyNo(loanapplyNo);
		o2oPaymentDetailEntity.setOperationName(user.getCname());
		o2oPaymentDetailEntity.setOperationNo(user.getNo());
		o2oPaymentDetailEntity.setPaymentType(o2oPaymentForm.getPaymentType());
		o2oPaymentDetailEntity.setRecordTime(date);
		o2oPaymentDetailEntity.setUpdateTime(date);
		o2oPaymentDetailEntity.setSubjectType(o2oPaymentForm.getSubject());
		return o2oPaymentDetailEntity;
	}

}
